﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrowserGameFramework.Messaging.Base;
using BrowserGameFramework.Messaging.Interfaces;
using System.Data.SqlClient;
using BrowserGameFramework.DBConnector.SqlServer;
using System.Xml.XPath;
using BrowserGameFramework.Common.XmlHelper;

namespace BrowserGameFramework.Messaging
{
    public class MessageHandler
    {
        /// <summary>
        /// Sends a Message
        /// </summary>
        /// <param name="message">Message object</param>
        public void SendMessage(Message message)
        {
            
            SqlCommand cmd = new SqlCommand("SetMessage"); 
            cmd.Parameters.Add(message.InboxType.BuildInputInt32Param("InboxType"));
            cmd.Parameters.Add(message.Autor.BuildInputInt32Param("Sender"));
            cmd.Parameters.Add(message.Recipient.BuildInputInt32Param("Recipient"));
            cmd.Parameters.Add(message.HeaderText.BuildInputStringParam("Header"));
            cmd.Parameters.Add(message.Priority.BuildInputInt32Param("Priority"));
            cmd.Parameters.Add(message.Text.BuildInputStringParam("Text"));

            SqlDbHandler handler = SqlDbHandler.GetInstance();
            handler.CallProcedureExecute(cmd);
        }

        /// <summary>
        /// Gets a Message from Database
        /// </summary>
        /// <param name="messageId">Message ID</param>
        /// <returns>Message Object</returns>
        public Message GetMessage(int messageId)
        {
            Message msg = new Message();

            SqlCommand cmd = new SqlCommand("GetMessage");
            cmd.Parameters.Add(messageId.BuildInputInt32Param("messageId"));

            SqlDbHandler handler = SqlDbHandler.GetInstance();
            XPathDocument xDoc = handler.CallXmlProcedureSelect(cmd);
            object ob = XmlHelper.GetObjectFromXml(xDoc, typeof(Message));
            msg = (Message)ob;

            return msg;
        }

        /// <summary>
        /// Gets a list of all messages from Database
        /// </summary>
        /// <param name="userId">Id of User</param>
        /// <returns>Messageheader for user without longtext</returns>
        public MessageHeaderCollection GetInboxMessagesForUser(int userId)
        {
            SqlCommand cmd = new SqlCommand("GetMessageForUser");
            cmd.Parameters.Add(userId.BuildInputInt32Param("userId"));

            SqlDbHandler handler = SqlDbHandler.GetInstance();
            XPathDocument xDoc = handler.CallXmlProcedureSelect(cmd);

            object ob = XmlHelper.GetObjectFromXml(xDoc, typeof(MessageHeaderCollection));
            MessageHeaderCollection collection = (MessageHeaderCollection)ob;

            return collection;
        }

        /// <summary>
        /// Gets a list of all messages wich where send by user
        /// </summary>
        /// <param name="userId">User wich send the messages</param>
        /// <returns>Collection with MessageHeader</returns>
        public MessageHeaderCollection GetOutBoxMessagesForUser(int userId)
        {
            SqlCommand cmd = new SqlCommand("GetMessageSendByUser");
            cmd.Parameters.Add(userId.BuildInputInt32Param("userId"));

            SqlDbHandler handler = SqlDbHandler.GetInstance();
            XPathDocument xDoc = handler.CallXmlProcedureSelect(cmd);

            object ob = XmlHelper.GetObjectFromXml(xDoc, typeof(MessageHeaderCollection));
            MessageHeaderCollection collection = (MessageHeaderCollection)ob;

            return collection;
        }

        /// <summary>
        /// Set the message read flag
        /// </summary>
        /// <param name="messageId">Message Identification Number</param>
        public void SetMessageAsRead(int messageId)
        {
            SqlCommand cmd = new SqlCommand("SetMessageAsRead");
            cmd.Parameters.Add(messageId.BuildInputInt32Param("messageId"));

            SqlDbHandler handler = SqlDbHandler.GetInstance();
            handler.CallProcedureExecute(cmd);
        }
    }
}
